package com.data.center.controller;

import cn.hutool.core.bean.BeanUtil;
import com.data.center.dto.PersonDto;
import com.data.center.dto.StaffDto;
import com.data.center.entity.Person;
import com.data.center.exception.ResultBody;
import com.data.center.service.PersonService;
import com.data.center.service.StaffService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2023-12-05 03:29:24
 */
@RestController
@RequestMapping("/person")
@Api(tags = "科技人才")
public class PersonController {

    @Autowired
    PersonService personService;

    @PostMapping("/findPerson")
    public ResultBody findPerson(@RequestBody PersonDto personDto) {
        return ResultBody.success(personService.findPerson(personDto));
    }

    @PostMapping("/addPerson")
    public ResultBody addPerson(@RequestBody PersonDto personDto) {
        try {
            return ResultBody.success(personService.addPerson(personDto));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/forceAddPerson")
    public ResultBody forceAddPerson(@RequestBody PersonDto personDto) {
        try {
            return ResultBody.success(personService.forceAddPerson(personDto));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/updatePerson")
    public ResultBody updatePerson(@RequestBody PersonDto updatePerson) {
        Person originalPerson = personService.getPersonById(updatePerson.getPersonId());
        if (originalPerson == null) {
            return ResultBody.error("人员不存在");
        }
        try {
            return ResultBody.success(personService.updatePerson(BeanUtil.copyProperties(originalPerson, PersonDto.class), updatePerson));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/logicalDelete")
    public  ResultBody logicalDelete(@RequestBody PersonDto personDto) {
        try {
            return ResultBody.success(personService.logicalDelete(personDto.getPersonId()));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/physicalDelete")
    public  ResultBody physicalDelete(@RequestBody PersonDto personDto) {
        try {
            return ResultBody.success(personService.physicalDelete(personDto.getPersonId()));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/merge")
    public  ResultBody merge(@RequestBody PersonDto personDto, @RequestBody PersonDto deletedPerson) {
        try {
            return ResultBody.success(personService.merge(personDto, deletedPerson));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }
}
